package cn.com.zsc.system;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.zsc.system.vo.PropertyVO;
import cn.com.zsc.util.ConstValue;

import com.jarp.db.DAO;

public class SelectPropertyType {

	public String getComboBox(String plCode) {
		String sql = "select CL_CODE, CL_NAME from cm_commlist where PL_Code="
				+ plCode;

		DAO dao = new DAO();
		Vector<Hashtable<String, String>> v = new Vector<Hashtable<String, String>>();

		Vector<Hashtable<String, String>> vresult = new Vector<Hashtable<String, String>>();
		Hashtable<String, String> hashtable = null;
		String resultStr = "";

		try {
			v = dao.getDataSet(ConstValue.DBNAME, sql);

			if (v != null) {
				for (int i = 0; i < v.size(); i++) {
					Hashtable hash = v.get(i);
					hashtable = new Hashtable<String, String>();
					if (hash.get("CL_NAME").equals("启用")) {
						hashtable.put("CL_CODE", "1");
						hashtable.put("CL_NAME", "启用");
					} else {
						hashtable.put("CL_CODE", "0");
						hashtable.put("CL_NAME", "停用");
					}

					vresult.add(hashtable);
				}
				JSONArray json = new JSONArray();
				json = JSONArray.fromObject(vresult);
				resultStr = json.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ConstValue.EMPTY_RECORD_NOT_PAGEING;
		}

		return resultStr;
	}

	/**
	 * 获取对应条数的字典信息
	 * @param start
	 * @param limit
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getPropertyType(String start, String limit) {
		DAO dao = new DAO();

		String sql = "select PL_CODE,PL_NAME,REMARKED,STATUS from cm_proplist order by PL_CODE desc limit " + start + "," + limit;
	
		String resultStr = ConstValue.EMPTY_RECORD_PAGEING;
		
		List list = new ArrayList();
		
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);
			
			if (v != null && v.size() > 0) {
				for (int i = 0; i < v.size(); i++) {
					PropertyVO dataVO = new PropertyVO();
					Hashtable cubeTable = (Hashtable) v.get(i);
					
					dataVO.setPlCode((String) cubeTable.get("PL_CODE"));
					dataVO.setPlName((String) cubeTable.get("PL_NAME"));
					dataVO.setStatus((String) cubeTable.get("STATUS"));
					dataVO.setRemarked((String) cubeTable.get("REMARKED"));
					
					list.add(dataVO);
				}
				
				JSONArray json = JSONArray.fromObject(list);
				
				String propertyStr = json.toString();
				
				sql = "select count(PL_CODE) from cm_proplist";
				
				String total = dao.getDataValue(ConstValue.DBNAME, sql);
				
				resultStr = "{\"totalCount\":\"" + total + "\",\"results\":" + propertyStr + "}";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return resultStr;
	}

	/**
	 * 获取全部字典信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String getPropertyType() {
		String sql = "select PL_CODE, PL_NAME, REMARKED, STATUS from cm_proplist";
		
		DAO dao = new DAO();
		
		List list = new ArrayList();
		
		String resultStr = ConstValue.EMPTY_RECORD_NOT_PAGEING;
	
		try {
			Vector<Hashtable<String, String>> v = dao.getDataSet(ConstValue.DBNAME, sql);
			
			if (v != null) {
				for (int i = 0; i < v.size(); i++) {
					PropertyVO dataVO = new PropertyVO();
					Hashtable cubeTable = (Hashtable) v.get(i);
					dataVO.setPlCode((String) cubeTable.get("PL_CODE"));
					dataVO.setPlName((String) cubeTable.get("PL_NAME"));
					dataVO.setStatus((String) cubeTable.get("STATUS"));
					dataVO.setRemarked((String) cubeTable.get("REMARKED"));
					list.add(dataVO);
				}
				
				JSONArray json = JSONArray.fromObject(list);
				resultStr = json.toString();
			} 
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		return resultStr;
	}

	public String insertProptype(String jsinfo) {
		DAO dao = new DAO();
		JSONObject jso = new JSONObject();
		jso = JSONObject.fromString(jsinfo);
		String restult = "";
		String name = jso.getString("plName").trim();
		String status = jso.getString("status").trim();
		String order = jso.getString("remarked");
		try {
			String sql = "insert into cm_proplist(PL_NAME,REMARKED,STATUS) "
					+ "values('" + name + "','" + order + "','" + status + "')";
			int a = dao.execCommand(ConstValue.DBNAME, sql);
			if (a > 0)
				return "success";
		} catch (Exception ex) {
			ex.printStackTrace();
			return "数据库操作失败";
		} finally {
			dao = null;
		}

		return restult;
	}

	public String delPropType(String jso) {
		JSONObject jsobject = JSONObject.fromObject(jso);
		
		String uid = jsobject.getString("plCode");
		
		DAO dao = new DAO();

		ArrayList<String> sqlArray = new ArrayList<String>();
		
		String del_commlist = "delete from cm_commlist where PL_CODE=" +uid;
		String del_proplist = "delete from cm_proplist where PL_CODE=" + uid;
		
		sqlArray.add(del_commlist);
		sqlArray.add(del_proplist);
		
		try {
			if (dao.execCommand(ConstValue.DBNAME, sqlArray)) {
				return "success";
			} else {
				return "数据库操作失败";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return "数据库操作失败";
		}
	}

	public String updateType(String jsinfo) {
		JSONObject jso = new JSONObject();
		jso = JSONObject.fromObject(jsinfo);
		String catalId = jso.getString("plCode").trim();
		String catalName = jso.getString("plName").trim();
		String order = jso.getString("remarked").trim();
		String status = jso.getString("status").trim();
		DAO dao = new DAO();
		String sql = "update cm_proplist set PL_NAME='" + catalName
				+ "', REMARKED='" + order + "',STATUS='" + status
				+ "' where PL_CODE='" + catalId + "'";
		try {
			if (dao.execCommand(ConstValue.DBNAME, sql) > 0) {
				return "success";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			return "数据库操作失败";
		} finally {
			dao = null;
		}

		return "";
	}

}
